// Program: ext2
// Description: Provides an interface for mounting an ext2-formatted device
//              into the filesystem.
//

//
// Ext2Filesystem.h -- Defines the interface for operations on an ext2
//                     filesystem.
//
#ifndef EXT2FILESYSTEM_H
#define EXT2FILESYSTEM_H

#include <jimix/Filesystem.h>
#include <jimix/resource.h>
#include <stdio.h>
#include "ext2.h"

class Ext2Filesystem : Callable
{
public:
  
  Ext2Filesystem();

  ~Ext2Filesystem();

  /**
    Populates this filesystem from the specified device and mounts it at 
    the given mount path. Returns zero if the mount succeeded, else returns
    a POSIX error code.
  **/
  int mount(const char *device, const char *path);
  
  /**
    Returns an array of resources describing the contents of the
    directory given by inode. The array and File* ptrs should all be made
    on the user heap.
  **/
  void populateDirectory(u32int inode, u32int directoryResource);
  
  u32int getSize(u32int inode);
  u32int read(u32int inode, u32int offset, u32int size, u8int *buffer);
  
  u32int resource;
  
private:
  
  inode_t getInode(u32int inode);  
  
  void readInode(inode_t inode, u8int *buffer, u32int offset=0, u32int size=0);
  
  void readBlock(u32int block, u8int *buffer, u32int limitSize=0);
  
  void getIndirectBlockAddresses(u32int blockn, u32int *blocks, u32int start, u32int number);
  
  void getBiindirectBlockAddresses(u32int blockn, u32int *blocks, u32int start, u32int number);
  
  /**
    Our interface to the kernel's filesystem.
  **/
  u32int filesystem;
  
  /**
    Filesystem superblock.
  **/
  superblock_t superblock;
  
  /**
    Group descriptors.
  **/
  group_desc_t *groupDescriptors;
  
  /**
    File descriptor for accessing device.
  **/
  FILE *fd;
};



#endif
